package com.easyedu.signcenter.controller;

import com.easyedu.core.util.R;
import com.easyedu.signcenter.entity.DO.User;
import com.easyedu.signcenter.entity.dto.request.ReqLogonMsg;
import com.easyedu.signcenter.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;


/**
 * @author lmr
 **/
@RequestMapping("/root")
@RestController
public class LoginController {

    private final UserService userService;

    public LoginController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 功能描：登录
     *
     * @param map 登录数据(userNumber 用户编号; password 密码)
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Map<String, Object> map) {
        Long userNumber = Long.valueOf(String.valueOf(map.get("userNumber")));
        String password = String.valueOf(map.get("password"));
        Map<String, Object> loginMsg = userService.login(userNumber, password);
        return R.ok().data(loginMsg);
    }

    /**
     * 功能描：退出登录
     *
     * @param userNumber 1 用户编号
     * @return
     */
    @GetMapping("/logout")
    public R logout(@RequestParam("userNumber") Long userNumber) {
        userService.logout(userNumber);
        return R.ok().message("退出登录成功！");
    }

    /**
     * 功能描：用户注册
     *
     * @param reqLogonMsg 注册信息 Persistent Object
     * @return
     */
    @PostMapping("/logon")
    public R logon(@RequestBody @Valid ReqLogonMsg reqLogonMsg) {
        User newUser = userService.logon(reqLogonMsg.getUserNumber(), reqLogonMsg.getUserName(), reqLogonMsg.getPassword());
        return R.ok().data(newUser);
    }

    /**
     * 功能描：检查用户名是否存在
     *
     * @param userName 用户名
     * @return
     */
    @GetMapping("/check/user/name")
    public R checkHasUser(@RequestParam String userName) {
        boolean hasUserName = userService.checkHasUserName(userName);
        return R.ok().data(hasUserName);
    }

    /**
     * 功能描：检查用户编号是否存在
     *
     * @param userNumber 用户编号
     * @return
     */
    @GetMapping("/check/user/number")
    public R checkHasUserNumber(@RequestParam Long userNumber) {
        boolean hasUserNumber = userService.checkHasUserNumber(userNumber);
        return R.ok().data(hasUserNumber);
    }
}
